package com.yyplatform.admin.platform.notice.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.yyplatform.admin.platform.notice.model.entity.SysNoticeUserEntity;

/**
 * Created on 2024-09-14
 */
public interface SysNoticeUserRepository extends JpaRepository<SysNoticeUserEntity, Long>,
        JpaSpecificationExecutor<SysNoticeUserEntity> {

    List<SysNoticeUserEntity> findByNoticeId(Long noticeId);


    @Query(
            value =
                    "SELECT * FROM sys_notice_user WHERE 1=1 "
                            + " AND if(:readFlag IS NOT NULL , `read_flag` = :readFlag, 1=1 ) "
                            + " AND if(:deleteFlag IS NOT NULL , `delete_flag` = :deleteFlag, 1=1 ) "
                            + " AND if(:noticeType IS NOT NULL , `notice_type` = :noticeType, 1=1 ) "
                            + " AND if(:userId IS NOT NULL , `user_id` = :userId, 1=1 ) "
                            + " AND (create_time >= :beginDate AND create_time <= :endDate) ",
            countQuery =
                    "SELECT count(id) FROM sys_notice_user WHERE 1=1 "
                            + " AND if(:readFlag IS NOT NULL , `read_flag` = :readFlag, 1=1 ) "
                            + " AND if(:deleteFlag IS NOT NULL , `delete_flag` = :deleteFlag, 1=1 ) "
                            + " AND if(:noticeType IS NOT NULL , `notice_type` = :noticeType, 1=1 ) "
                            + " AND if(:userId IS NOT NULL , `user_id` = :userId, 1=1 ) "
                            + " AND (create_time >= :beginDate AND create_time <= :endDate) ",
            nativeQuery = true)
    Page<SysNoticeUserEntity> findPage(
            @Param("readFlag") Boolean readFlag,
            @Param("deleteFlag") Boolean deleteFlag,
            @Param("noticeType") Integer noticeType,
            @Param("userId") Long userId,
            @Param("beginDate") Date beginDate,
            @Param("endDate") Date endDate,
            Pageable pageable);


    List<SysNoticeUserEntity> findByUserId(Long userId);

}
